package com.ricchieri.manage.bean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleModel;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.common.CustomScheduleEvent;
import com.ricchieri.filter.ReservationFilter;
import com.ricchieri.filter.ScheduleCharterFilter;
import com.ricchieri.lazymodel.ScheduleLazyModel;
import com.ricchieri.lazymodel.ScheduleStatusLazyDataModel;
import com.ricchieri.model.Reservation;
import com.ricchieri.model.ScheduleCharter;
import com.ricchieri.reports.ReportFactory;
import com.ricchieri.reports.ReportFileFormat;
import com.ricchieri.reports.ReportTemplate;
import com.ricchieri.service.interf.IReservationService;
import com.ricchieri.service.interf.IScheduleCharterService;
import com.ricchieri.service.interf.ITripService;
import com.ricchieri.util.FacesUtil;
import com.ricchieri.util.StringUtils;

@ManagedBean(name = "viewScheduleStatusMB")
@ViewScoped
public class ViewScheduleStatusManageBean extends GenericManageBean {
	private static final long serialVersionUID = -7001021047911879866L;

	@ManagedProperty(value = "#{TripService}")
	ITripService tripService;

	@ManagedProperty(value = "#{ScheduleCharterService}")
	IScheduleCharterService scheduleCharterService;
		
	@ManagedProperty(value = "#{ReservationService}")
	IReservationService reservationService;

	private ScheduleCharter scheduleCharter;
	private Reservation reservation;
	private String message = "";
	
	private Map<String, String> trips = null;

	private ScheduleCharterFilter scheduleCharterFilter = new ScheduleCharterFilter();
	private ScheduleModel eventModel = new DefaultScheduleModel();
	private CustomScheduleEvent event = new CustomScheduleEvent();
	private ScheduleLazyModel scheduleLazyModel;

	public void setTrips(Map<String, String> trips) {
		this.trips = trips;
	}

	public ITripService getTripService() {
		return tripService;
	}

	public void setTripService(ITripService tripService) {
		this.tripService = tripService;
	}

	public Map<String, String> getTrips() {
		if (trips == null) {
			trips = getTripService().getComboTrips();
		}

		return trips;
	}

	public CustomScheduleEvent getEvent() {
		return event;
	}

	public void setEvent(CustomScheduleEvent event) {
		this.event = event;
	}

	public void onEventSelect(SelectEvent selectEvent) {
		event = (CustomScheduleEvent) selectEvent.getObject();;
	}

	public ScheduleModel getEventModel() {
		return eventModel;
	}

	public void setEventModel(ScheduleModel eventModel) {
		this.eventModel = eventModel;
	}


	public IScheduleCharterService getScheduleCharterService() {
		return scheduleCharterService;
	}

	public void setScheduleCharterService(
			IScheduleCharterService scheduleCharterService) {
		this.scheduleCharterService = scheduleCharterService;
	}


	public ScheduleCharterFilter getScheduleCharterFilter() {
		return scheduleCharterFilter;
	}

	public void setScheduleCharterFilter(
			ScheduleCharterFilter scheduleCharterFilter) {
		this.scheduleCharterFilter = scheduleCharterFilter;
	}

	public ScheduleLazyModel getScheduleLazyModel() {

		if (scheduleLazyModel == null) {
			scheduleLazyModel = new ScheduleStatusLazyDataModel(
					getScheduleCharterService(), getScheduleCharterFilter(),
					getEventModel());
		}
		return scheduleLazyModel;
	}

	public void setScheduleLazyModel(ScheduleLazyModel scheduleLazyModel) {
		this.scheduleLazyModel = scheduleLazyModel;
	}

	public void changeTrip() {
		setScheduleLazyModel(null);
		setEventModel(new DefaultScheduleModel());
	}
	
	public void showTravelDetails(CustomScheduleEvent event) {
		
		scheduleCharter = event.getScheduleCharter();
	}
	
	public ScheduleCharter getScheduleCharter() {
		return scheduleCharter;
	}

	public void setScheduleCharter(ScheduleCharter scheduleCharter) {
		this.scheduleCharter = scheduleCharter;
	}
	
	public void resetSendMessage() {
		message = "";
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public void sendNotification(){
		Boolean isValid = Boolean.TRUE;
		
		if(StringUtils.isEmpty(message)) {
			isValid = Boolean.FALSE;
			FacesUtil.addErrorMsg("addSchedule:message", "requiredMessageSent");
		} else {
			FacesUtil.showInfoMessage("viewScheduleCharterStatus","sentWarning");
			getReservationService().sendNotification(scheduleCharter, message);
			resetSendMessage();
		}
		
		RequestContext context = RequestContext.getCurrentInstance();
		context.addCallbackParam("isValid", isValid);
	}

	public IReservationService getReservationService() {
		return reservationService;
	}

	public void setReservationService(IReservationService reservationService) {
		this.reservationService = reservationService;
	}
	
	public void downloadReport() {
		ReportTemplate report = ReportFactory.getReport(BusinessSettings.SCHEDULE_CHARTER_STATUS_REPORT);
		Map<String, Object> map = new HashMap<String, Object>();		
		map.put(BusinessSettings.KEY_SCHEDULE_CHARTER, getScheduleCharter());
		map.put(BusinessSettings.KEY_SERVICE, getScheduleCharterService());
		report.generateReport(map , ReportFileFormat.PDF);
	}
	
	public String obtainTextToSend() {
		if(scheduleCharter.getReservation() != null && scheduleCharter.getReservation().size() > 0) {
			reservation = scheduleCharter.getReservation().get(0);
		}
		
		if(reservation !=  null) {
			return getReservationService().replaceTextEmailWithVariables(reservation, getMessage());
		}
		
		return "";
	}
	
	public String getStatusDescription(Boolean status) {
		if(status) {
			return FacesUtil.getMessage("reservationCanceled");
		} else {
			return FacesUtil.getMessage("reservationActive");
		}
	}
	
	public Boolean isAfterLastUpdate(Reservation reservation) {
		
		if(getScheduleCharter().getLastReportUpdate() == null) {
			return Boolean.FALSE;
		}
		
		if(reservation.getCancel()) {
			return getScheduleCharter().getLastReportUpdate().before(reservation.getCancelationMotive().getCancelationDate());
		} else {
			return getScheduleCharter().getLastReportUpdate().before(reservation.getReservationDate());
		}
	}
	
	public List<Reservation> orderReservations() {
		ReservationFilter reservationFilter = new ReservationFilter();
		reservationFilter.setScheduleCharterId(this.getScheduleCharter().getId());
		reservationFilter.setSortField("sortFullname");
		reservationFilter.setSortOrder(BusinessSettings.SORT_ASCENDING);
		
		this.getScheduleCharter().setReservation(this.getReservationService().getAllReservationByFilter(reservationFilter));
		
		return this.getScheduleCharter().getReservation();
	}

}
